
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GLES/gl.h> /* For NDK*/
#include <android/log.h>
#include "utils.h"
#include "jade.h"


///////////////////////////////////////////////////////////////////////////////
//OpenGL API

void gluPerspective(GLfloat fovy, GLfloat aspect,
                           GLfloat zNear, GLfloat zFar)
{
    GLfloat xmin, xmax, ymin, ymax;

    ymax = zNear * (GLfloat)tan(fovy * PI / 360);
    ymin = -ymax;
    xmin = ymin * aspect;
    xmax = ymax * aspect;

    glFrustumx((GLfixed)(xmin * 65536), (GLfixed)(xmax * 65536),
               (GLfixed)(ymin * 65536), (GLfixed)(ymax * 65536),
               (GLfixed)(zNear * 65536), (GLfixed)(zFar * 65536));
}


///////////////////////////////////////////////////////////////////////////////
//World

jade_world_t* 
jade_world_create (int width, int height, 
        GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha){

    jade_world_t *world;

    world = (jade_world_t*)malloc (sizeof (jade_world_t));
    if (world == NULL)
        return NULL;

    world->m_width = width;
    world->m_height = height;
    world->m_bgred = red;
    world->m_bggreen = green;
    world->m_bgblue = blue;
    world->m_bgalpha = alpha;
    world->m_fovy = 15.0f;
    world->m_znear = 0.5f;
    world->m_zfar = 150.0f;
    world->m_camera = NULL;


    glViewport(0, 0, width, height);
    //fovy, aspect, zNear, zFar
    gluPerspective(world->m_fovy, (float)width / height, world->m_znear, world->m_zfar);
    //gluPerspective(15.0f, (float)width / height, 0.5f, 150.0f);


    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity();

    LOGD ("jade_world_create: red = %f, green = %f, blue =%f, alpha = %f.\n", red, green, blue, alpha);

#ifdef FLOAT_ARG_FAIL
    glClearColor(255.0f, 0.0f, 0.0f, 0.0f);
#else
    glClearColor(red, green, blue, alpha);
#endif
    
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glShadeModel(GL_SMOOTH);


    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); //GL_FASTEST GL_NICEST

    return world;
}

void jade_world_destroy (jade_world_t *world) {
/* 
    //world do not free camera.   
    if (world->m_camera != NULL)
        FREEANDNULL (world->m_camera);
*/
    FREEANDNULL (world);   
}

void jade_world_set_camera (jade_world_t *world, jade_camera_t *camera) {
    
    world->m_camera = camera;

}

void jade_world_prepare_render(jade_world_t* world)
{

///    glViewport(0, 0, width, height);
/*
    glClearColorx((GLfixed)(0.1f * 65536),
                  (GLfixed)(0.2f * 65536),
                  (GLfixed)(0.3f * 65536), 0x10000);
*/
    glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    gluPerspective(world->m_fovy, (float)world->m_width / world->m_height, world->m_znear, world->m_zfar);
    //gluPerspective(15, (float)world->m_width / world->m_height, 0.5f, 150);
    //gluPerspective(60, (float)width / height, 1.0f, 1000);
    //gluPerspective(45, (float)width / height, 0.5f, 150);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

/*
    gluLookAt(0.0f, 0.0f, 30.0f, 0.0f, 0.0f, 0.0f,
	          0.0f, 1.0f, 0.0f);
*/
    jade_camera_glulookat (world->m_camera);
}

